﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Taobao.Top.Api.Domain;
using Taobao.Top.Api.Request;
using Taobao.Top.Api;
using TopHelper;
using System.IO;
using System.Text;

public partial class SellerRateDetails : System.Web.UI.Page
{
    private TopXmlRestClient topClient = Common.CreateTopClient();
    private int page = 0;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            
            if (Session["sessionKey"] == null)
            {
                Response.Redirect("http://container.open.taobao.com/container?appkey=12112146", false);
            }
            else
            {
                if (Request.QueryString["page"] != null)
                {
                    page = Convert.ToInt32(Convert.ToDouble(Request.QueryString["page"]));
                }
            }
            if (Request.QueryString["rateType"] != null)
            {
                Session["rateType"] = Request.QueryString["rateType"].ToString();
                
            }
            GetData();
            StringWriter writer = new StringWriter();
            HtmlTextWriter rateHtml = new HtmlTextWriter(writer);
            repRateDetails.RenderControl(rateHtml);
            Response.Write(rateHtml.InnerWriter);
            Response.End();
        }
    }
    protected void GetData()
    {
        repRateDetails.DataSource = GetRatesData();
        repRateDetails.DataBind();
    }
    protected List<RateDetails> GetRatesData()
    {
        List<RateDetails> details = new List<RateDetails>();
        TraderatesGetRequest rateReq = new TraderatesGetRequest();
        rateReq.Fields = "item_title,tid,result,created,content";
        if (page != 0)
        {
            rateReq.PageNo = page;
        }
        rateReq.PageSize = 5;
        if (Session["rateType"] != null)
        {
            rateReq.Result = Session["rateType"].ToString();
        }
        rateReq.RateType = "get";
        rateReq.Role = "buyer";
        PageList<TradeRate> rateRsp = new PageList<TradeRate>();
        Session["pageCount"] = rateRsp.TotalResults;
        try
        {
            rateRsp = topClient.TraderatesGet(rateReq, Session["sessionKey"].ToString());
            TradeFullinfoGetRequest tradeReq = new TradeFullinfoGetRequest();
            Trade tradeInfo = new Trade();
            long tid = 0;
            foreach (TradeRate rate in rateRsp.Content)
            {
                RateDetails detail = new RateDetails();
                if (!string.Equals(tid, rate.Tid))
                {
                    tradeReq.Tid = rate.Tid;
                    tradeReq.Fields = "buyer_nick,receiver_state,receiver_city,receiver_district,receiver_address,receiver_mobile,receiver_name";
                    tradeInfo = topClient.TradeFullinfoGet(tradeReq, Session["sessionKey"].ToString());
                    tid = tradeInfo.Tid;
                }

                detail.Tid = rate.Tid;
                detail.BuyerNick = tradeInfo.BuyerNick;
                detail.Result = rate.Result;
                detail.Content = rate.Content;
                detail.ItemTitle = rate.ItemTitle;
                detail.ReceiverName = tradeInfo.ReceiverName;
                detail.ReceiverMobile = tradeInfo.ReceiverMobile;
                detail.BuyerAddress = tradeInfo.ReceiverState + tradeInfo.ReceiverCity + tradeInfo.ReceiverDistrict + tradeInfo.ReceiverAddress;
                details.Add(detail);
            }
        }
        catch (TopException e)
        {
            throw;
        }
        return details;
    }
}
